﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using PlanningGIS.Win.Controls;

namespace PlanningGIS.Cartographic
{
    //自定义范围打印设置窗体
    public partial class FrmCustomPrintSetting : Office2007Form
    {
        private IMapControl3 m_MapControl; //地图控件
        private IEnvelope m_pPrintArea; //打印范围

        public IMapControl3 MapControlOutProp
        {
            get { return m_MapControl; }
            set { m_MapControl = value; }
        }

        public IEnvelope PrintAreaProp
        {
            get { return m_pPrintArea; }
            set { m_pPrintArea = value; }
        }

        public FrmCustomPrintSetting()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)
            {
                cboScale.Enabled = true;
                cboPageSize.Enabled = false;
            }
            else
            {
                cboScale.Enabled = false;
                cboPageSize.Enabled = true;
            }
        }

        private void FrmCustomPrintSetting_Load(object sender, EventArgs e)
        {
            radioButton1.Checked = true;
            //列出常用比例尺
            cboScale.Items.Add("1:500");
            cboScale.Items.Add("1:1000");
            cboScale.Items.Add("1:2000");
            cboScale.Items.Add("1:5000");
            cboScale.Items.Add("1:10000");
            cboScale.Items.Add("1:50000");
            cboScale.Items.Add("1:250000");

            //默认输出500比例尺
            cboScale.Text = "1:500";

            //列出esriPageFormID枚举的页面常数
            //comboBox的SelectedIndex即为当前FormID
            cboPageSize.Items.Add("Letter - 8.5in x 11in. ");
            cboPageSize.Items.Add("Legal - 8.5in x 14in.");
            cboPageSize.Items.Add("Tabloid - 11in x 17in.");
            cboPageSize.Items.Add("C - 17in x 22in.");
            cboPageSize.Items.Add("D - 22in x 34in.");
            cboPageSize.Items.Add("E - 34in x 44in.");
            cboPageSize.Items.Add("A5 - 148mm x 210mm.");
            cboPageSize.Items.Add("A4 - 210mm x 297mm.");
            cboPageSize.Items.Add("A3 - 297mm x 420mm.");
            cboPageSize.Items.Add("A2 - 420mm x 594mm.");
            cboPageSize.Items.Add("A1 - 594mm x 841mm.");
            cboPageSize.Items.Add("A0 - 841mm x 1189mm.");

            //默认输出A4大小
            cboPageSize.Text = "A4 - 210mm x 297mm.";
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            double dScale = 0; //制图比例尺
            int iPageID = 0; //打印页面大小参数
            short iOri = 0; //页面方向

            if (cboScale.Enabled == true) //按用户指定比例尺打印,默认页面设置为A4纵向
            {
                try
                {
                    dScale = Convert.ToDouble(cboScale.Text.Substring(2));
                }
                catch
                {
                    MsgHelper.ShowWarningMsgbox("输入的比例尺异常");
                    return;
                }

                iOri = 1;
                iPageID = 7;
            }
            else //按用户选择的页面大小打印,并根据当前用户输入的打印范围计算制图比例尺大小
            {
                //comboBox的SelectedIndex即为当前FormID
                iPageID = cboPageSize.SelectedIndex;

                IPage pPage = new PageClass();
                pPage.FormID = (esriPageFormID)iPageID;

                //设置单位为厘米
                pPage.Units = ESRI.ArcGIS.esriSystem.esriUnits.esriCentimeters;

                double dPageW = 0; //页宽
                double dPageH = 0; //页宽
                //QuerySize接口计算当前选择的页面的大小的宽度、高度
                pPage.QuerySize(out dPageW ,out dPageH);

                double dPBP = 0.05; /// 页边距与页宽的比值
                dPageW = dPageW - dPageW * dPBP * 2;
                dPageH = dPageH - dPageW * dPBP * 2;

                if (m_pPrintArea.Width < m_pPrintArea.Height)
                {
                    //打印区域宽度小于高度，设置页面方向为纵向
                    iOri = 1;

                    if (m_pPrintArea.Width / m_pPrintArea.Height <= dPageW / dPageH)
                    {
                        //打印区域宽高比小于等于页面宽高比，按打印区域高与页高比值计算制图比例尺
                        dScale = m_pPrintArea.Height / dPageH * 100;
                    }
                    else
                    {
                        //打印区域宽高比大于页面宽高比，按打印区域宽与页宽比值计算制图比例尺
                        dScale = m_pPrintArea.Width / dPageW * 100;
                    }
                }
                else
                {
                    //打印区域宽度大于等于高度，设置页面方向为横向
                    iOri = 2;

                    if (m_pPrintArea.Width / m_pPrintArea.Height <= dPageH / dPageW)
                    {
                        //打印区域宽高比小于等于页面宽高比，按打印区域高与页宽比值计算制图比例尺
                        dScale = m_pPrintArea.Height / dPageW * 100;
                    }
                    else
                    {
                        //打印区域宽高比大于页面高宽比，按打印区域宽与页高比值计算制图比例尺
                        dScale = m_pPrintArea.Width / dPageH * 100;
                    }
                }
            }

            FrmCustomPrint frm = FrmCustomPrint.CreateInstance();
            frm.MapControlOutProp = m_MapControl;
            frm.PrintAreaProp = m_pPrintArea;
            frm.ScaleProp = dScale;
            frm.OriProp = iOri;
            frm.PageIDProp = iPageID;
            frm.ShowDialog();

            this.Close();
        }
    }
}
